import datetime
from decimal import Decimal

from django.db.models import F,Q
from django.shortcuts import render

# Create your views here.
from rest_framework.views import APIView
from rest_framework.response import Response
from .sers import *

class LoginView(APIView):

    def post(self,request):
        username=request.POST.get('username')
        password=request.POST.get('password')
        user=UserModel.objects.filter(username=username,password=password).first()
        if user:
            return Response({'msg':'登录成功','code':200,'uid':user.id,'vip':user.vip})
        else:
            return Response({'msg':'登录失败','code':400})

class StartView(APIView):

    def get(self,request):
        teacher=TeacherModel.objects.all()
        ser=TeacherSers(teacher,many=True)
        return Response(ser.data)

    def put(self,request):
        uid=request.data.get('uid')
        id=request.data.get('id')
        TimeModel.objects.create(user_id=uid,teacher_id=id)
        return Response({'msg':'开始训练'})


class TeacherView(APIView):

    def get(self,request):
        id=request.GET.get('id')
        print(id)
        teacher=TeacherModel.objects.filter(id=id).first()
        ser=TeacherSers(teacher)
        return Response(ser.data)

class ShowView(APIView):

    def get(self,request):
        data = TeacherModel.objects.all()
        xin = TeacherSers(data,many=True)
        return Response(xin.data)

class EndView(APIView):

    def put(self,request):
        uid = request.data.get('uid')
        id = request.data.get('id')
        update=TimeModel.objects.filter(user_id=uid,teacher_id=id).last()
        user=UserModel.objects.filter(id=uid).first()
        start=update.start
        end=datetime.datetime.now()
        times=end-start
        hours=times.days*24+times.seconds/3600
        if user.vip==False:
            prices=hours*400
            if user.price<=400:
                return Response({'msg':'余额不足，请充值','code':100})
        else:
            prices = hours * 200
            if user.price<=200:
                return Response({'msg':'余额不足，请充值','code':100})
        prices=Decimal(prices)
        price1 = user.price - prices
        price1=round(price1,1)
        print(prices)
        print(hours)
        aaa=UserModel.objects.filter(id=uid).update(price=price1)
        # print(aaa)
        # print(prices)

        update.end=end
        print()
        update.money=prices
        update.counts+=1
        update.save()
        return Response({'msg':'离开成功'})

class LevelView(APIView):

    def put(self,request):
        uid=request.data.get('uid')
        user=UserModel.objects.filter(id=uid).first()
        if user.vip==True:
            return Response({'msg':'已经是会员'})
        else:
            if user.price<100:
                return Response({'msg':'余额不足，请充值','code':100})
            price=user.price-100
            UserModel.objects.filter(id=uid).update(price=price)
            UserModel.objects.filter(id=uid).update(vip=True)
            return Response({'msg':'升级成功'})

class StudyView(APIView):

    def get(self,request):
        ke=StudyModel.objects.all()
        ser=StudySers(ke,many=True)
        return Response(ser.data)

    def put(self,request):
        uid=request.data.get('uid')
        id=request.data.get('id')
        Time2Model.objects.create(user_id=uid,study_id=id)
        data=Time2Model.objects.filter(user_id=uid,study_id=id).last()  #默认选中最后一条
        user = UserModel.objects.filter(id=uid).first()
        if user.vip==False:
            prices=StudyModel.objects.filter(id=id).first().prices
            if user.price<=prices:
                return Response({'msg':'余额不足，请充值','code':100})
            moneys=user.price-prices
            print(moneys)
        else:
            prices = StudyModel.objects.filter(id=id).first().prices-50
            if user.price<=prices:
                return Response({'msg':'余额不足，请充值','code':100})
            moneys = user.price - prices-50
        UserModel.objects.filter(id=uid).update(price=moneys)

        print(moneys)
        data.money=prices
        data.save()

        return Response({'msg':'购买成功'})

class BuyView(APIView):

    def put(self,request):
        uid=request.data.get('uid')
        money=request.data.get('money')
        print(uid,money)
        UserModel.objects.filter(id=uid).update(price=F('price') + money)
        return Response({'msg':'充值成功'})

class Update1View(APIView):

    def get(self, request):
        id = request.GET.get('uid')
        print(id)
        update = TimeModel.objects.filter(user_id=id)
        ser = TimeSers(update,many=True)
        return Response(ser.data)

class Update2View(APIView):

    def get(self, request):
        id = request.GET.get('uid')
        print(id)
        update2 = Time2Model.objects.filter(user_id=id)
        ser = Time2Sers(update2,many=True)
        return Response(ser.data)

class Update3View(APIView):

    def get(self, request):
        id = request.GET.get('uid')
        print(id)
        update2 = Time2Model.objects.filter(user_id=id).first()
        ser = Time2Sers(update2)
        return Response(ser.data)

class SouView(APIView):
    def get(self,request):
        update=TimeModel.objects.filter(Q(counts__gt=3))
        ser=TimeSers(update,many=True)
        return Response(ser.data)
